package net.sea.setup.ui.wizard.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.ui.wizard.AbstractWizardPage;

/**
 * 发布地址配置页面
 * 
 * @author li_zhen
 *
 */
@SuppressWarnings("serial")
public class ReleaseAddressPage extends AbstractWizardPage {
	private JTextField ipTextField;
	private JSpinner portSpinner = new JSpinner();

	public ReleaseAddressPage() {
		setOpaque(false);
		setBorder("发布地址");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
		gridBagLayout.columnWidths = new int[] { 20, 100, 300, 100, 0 };
		setLayout(gridBagLayout);

		JLabel dbIPLb = new JLabel("发布地址IP：");
		GridBagConstraints gbc_dbIPLb = new GridBagConstraints();
		gbc_dbIPLb.gridwidth = 2;
		gbc_dbIPLb.anchor = GridBagConstraints.EAST;
		gbc_dbIPLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbIPLb.gridx = 0;
		gbc_dbIPLb.gridy = 5;
		add(dbIPLb, gbc_dbIPLb);

		ipTextField = new JTextField();

		GridBagConstraints gbc_ipTextField = new GridBagConstraints();
		gbc_ipTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_ipTextField.anchor = GridBagConstraints.NORTH;
		gbc_ipTextField.insets = new Insets(0, 0, 5, 5);
		gbc_ipTextField.gridx = 2;
		gbc_ipTextField.gridy = 5;
		add(ipTextField, gbc_ipTextField);
		ipTextField.setColumns(10);

		JLabel dbPortLb = new JLabel("发布地址端口：");
		GridBagConstraints gbc_dbPortLb = new GridBagConstraints();
		gbc_dbPortLb.gridwidth = 2;
		gbc_dbPortLb.anchor = GridBagConstraints.EAST;
		gbc_dbPortLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbPortLb.gridx = 0;
		gbc_dbPortLb.gridy = 6;
		add(dbPortLb, gbc_dbPortLb);

		portSpinner.setModel(new SpinnerNumberModel(9090, 1, 65535, 1));
		GridBagConstraints gbc_porSpinner = new GridBagConstraints();
		gbc_porSpinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_porSpinner.anchor = GridBagConstraints.NORTH;
		gbc_porSpinner.insets = new Insets(0, 0, 5, 5);
		gbc_porSpinner.gridx = 2;
		gbc_porSpinner.gridy = 6;
		add(portSpinner, gbc_porSpinner);

	}

	protected List<CustomConfig> createCustomConfig() {

		// 保存配置系统到系统上下文
		contextInst.setAttribute(AppConstants.CLIENT_RELEASE_ADDRESS,
				ipTextField.getText());
		contextInst.setAttribute(AppConstants.SERVER_RELEASE_ADDRESS_PORT,
				portSpinner.getValue());
		return null;
	}

	/**
	 * 判断是否为合法IP
	 * 
	 * @return true or false
	 */
	public static boolean isIpv4(String ipAddress) {
		String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		Pattern pattern = Pattern.compile(ip);
		Matcher matcher = pattern.matcher(ipAddress);
		return matcher.matches();
	}

	@Override
	public PageResult validateInput() {
		if (StringUtils.isBlank(ipTextField.getText())
				|| StringUtils.isBlank(String.valueOf(portSpinner.getValue()))) {
			return new PageResult(false, "IP不能为空！");
		}
		if (!isIpv4(ipTextField.getText())) {
			return new PageResult(false, "IP格式不正确！");
		}
		createCustomConfig();
		return new PageResult(true);
	}
}
